package com.shelpe.services.sinbad.controller.actionLog.impl.creative;

import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

import com.shelpe.services.sinbad.controller.actionLog.ActionLogDto;
import com.shelpe.services.sinbad.controller.actionLog.ActionLogService;
import com.shelpe.services.sinbad.controller.actionLog.CreativeVo;
import com.shelpe.services.sinbad.entity.jpa.Adgroup;
import com.shelpe.services.sinbad.entity.jpa.Campaign;
import com.shelpe.services.sinbad.entity.jpa.CreativeLog;

@Service
public class CreativeDelConverter extends ActionLogService {
	@Autowired
	private MessageSource messageSource;

	@Override
	public ActionLogDto convertCreative(CreativeLog entity, Map<Long, Campaign> campaignMap,
			Map<Long, Adgroup> adgroupMap) {
		boolean creativeDelConverter = entity.getAction() == CreativeLog.Action.AUTO_DEL
				|| entity.getAction() == CreativeLog.Action.MANUAL_DEL;
		if (creativeDelConverter) {
			if (StringUtils.isNotBlank(entity.getNote())) {
				ActionLogDto dto = new ActionLogDto();
				dto.from(entity);
				CreativeVo vo = this.unserializeCreative(entity.getNote());
				if (vo != null && StringUtils.isNotBlank(vo.getCreativeOldTitle())) {
					String[] params = { vo.getCreativeOldTitle() };
					dto.setReason(messageSource.getMessage("message.creative.del.reason", params, Locale.ENGLISH));
					return dto;
				}
			}
		}
		return null;
	}
}
